﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using common;
using System.Windows.Forms;
using Dapper;
using System.Data.SqlClient;
using DBEntity;
using DBClass;

namespace HTF_HMI
{
    public partial class AUDIT_TASK_QUERY : Form
    {
        AutoSizeFormClass asc = new AutoSizeFormClass();

        public AUDIT_TASK_QUERY()
        {
            InitializeComponent();
        }

        private void AUDIT_TASK_QUERY_Load(object sender, EventArgs e)
        {
            asc.controlAutoSize(this);
            BindArea();
            btn_query_Click(null, null);
        }

        private void BindArea()
        {
            var area = DBHelper.GetAuditArea();
            cmb_area.DisplayMember = "AREA_NAME";
            cmb_area.ValueMember = "AREA_ID";
            cmb_area.DataSource = area;

            System.Collections.ArrayList mylist = new System.Collections.ArrayList();
            mylist.Add(new System.Collections.DictionaryEntry("-1", "全部"));
            mylist.Add(new System.Collections.DictionaryEntry("0", "未处理"));
            mylist.Add(new System.Collections.DictionaryEntry("1", "已处理"));
            mylist.Add(new System.Collections.DictionaryEntry("2", "延期"));
            cmb_status.DataSource = mylist;
            cmb_status.DisplayMember = "Value";
            cmb_status.ValueMember = "Key";
        }

        public void btn_query_Click(object sender, EventArgs e)
        {
            if (cmb_status.SelectedValue.ToString() == "-1")
            {
                bindingSource1.DataSource = DBHelper.GetAuditPlan(cmb_area.Text, date_start.Text, date_end.Text, "");
            }
            else
            {
                bindingSource1.DataSource = DBHelper.GetAuditPlan(cmb_area.Text, date_start.Text, date_end.Text, cmb_status.SelectedValue.ToString());
            }
            if (gridView1.RowCount > 0)
            {
                gridView1.SelectRows(0, 0);
            }
        }


        private void btn_reset_Click(object sender, EventArgs e)
        {
            cmb_area.SelectedIndex = 0;
            btn_query_Click(null, null);
        }
      
        private void repositoryItemLookUpEdit1_EditValueChanged(object sender, EventArgs e)
        {
            object value = (sender as DevExpress.XtraEditors.LookUpEdit).EditValue;
            if (value != null)
            {
                string deviceName = DBHelper.GetAuditDeviceByDeviceID(value.ToString()).FirstOrDefault().DEVICE_NAME;
                string devicePosition = DBHelper.GetAuditDeviceByDeviceID(value.ToString()).FirstOrDefault().DEVICE_TYPE_NAME;
                this.gridView3.SetFocusedRowCellValue(gridView3.Columns["DEVICE_NAME"], deviceName);
                this.gridView3.SetFocusedRowCellValue(gridView3.Columns["DEVICE_TYPE_NAME"], devicePosition);

                bindingSource3.DataSource = DBHelper.GetAuditItemByDeviceID(value.ToString());
            }
        }

        private void gridView1_SelectionChanged(object sender, DevExpress.Data.SelectionChangedEventArgs e)
        {
            int[] rows = this.gridView1.GetSelectedRows();

            if (rows.Count() == 1)
            {
                if (this.gridView1.GetRowCellValue(rows[0], gridView1.Columns["PLANID"]) != null)
                {
                    string planId = this.gridView1.GetRowCellValue(rows[0], gridView1.Columns["PLANID"]).ToString();
                    bindingSource2.DataSource = DBHelper.GetAuditPlanDevice(planId);
                    bindingSource3.DataSource = null;
                    if (gridView3.RowCount > 0)
                    {
                        gridView3.SelectRows(0, 0);
                    }
                }
            }
        }

        private void gridView3_SelectionChanged(object sender, DevExpress.Data.SelectionChangedEventArgs e)
        {
            int[] rows = this.gridView3.GetSelectedRows();

            if (rows.Count() == 1)
            {
                string planId = this.gridView3.GetRowCellValue(rows[0], gridView3.Columns["PLANID"]).ToString();
                string deviceId = this.gridView3.GetRowCellValue(rows[0], gridView3.Columns["DEVICE_ID"]).ToString();
                bindingSource3.DataSource = DBHelper.GetAuditPlanItem(planId, deviceId);
            }
        }

        private void gridView5_CustomColumnDisplayText(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnDisplayTextEventArgs e)
        {
            if (e.Column.FieldName == "ITEMS_STATUS")
            {

                if (e.DisplayText == "1")
                    e.DisplayText = "√";
                else
                    e.DisplayText = "×";
            }

            if (e.Column.FieldName == "error_status")
            {

                if (e.DisplayText == "1")
                    e.DisplayText = "已处理";
                else
                    e.DisplayText = "未处理";
            }

            //string url = System.Configuration.ConfigurationManager.AppSettings["PicUrl"].ToString();
            //if (e.Column.FieldName == "ITEMS_PIC" || e.Column.FieldName == "ITEMS_PIC_HANDLE")
            //{
            //    e.DisplayText = url + e.DisplayText;
            //    DownloadFile(url + e.DisplayText, e.DisplayText);
            //}
        }

        /// <summary>
        /// 下载文件
        /// </summary>
        /// <param name="URL">下载文件地址</param>
        /// <param name="Filename">下载后另存为（全路径）</param>

        private bool DownloadFile(string URL, string filename)
        {
            try
            {
                System.Net.HttpWebRequest Myrq = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(URL);
                System.Net.HttpWebResponse myrp = (System.Net.HttpWebResponse)Myrq.GetResponse();
                System.IO.Stream st = myrp.GetResponseStream();
                System.IO.Stream so = new System.IO.FileStream(filename, System.IO.FileMode.Create);
                byte[] by = new byte[1024];
                int osize = st.Read(by, 0, (int)by.Length);
                while (osize > 0)
                {
                    so.Write(by, 0, osize);
                    osize = st.Read(by, 0, (int)by.Length);
                }
                so.Close();
                st.Close();
                myrp.Close();
                Myrq.Abort();
                return true;
            }
            catch (System.Exception e)
            {
                MessageBox.Show("下载文件失败！");         
                return false;
            }
        }

        private void gridView5_RowCellClick(object sender, DevExpress.XtraGrid.Views.Grid.RowCellClickEventArgs e)
        {
            if (e.Column.FieldName == "ITEMS_PIC" || e.Column.FieldName == "ITEMS_PIC_HANDLE")
            {
                if (e.CellValue == null)
                { return; }
                string url = System.Configuration.ConfigurationManager.AppSettings["PicUrl"].ToString();
                string file = e.CellValue.ToString();
                DownloadFile(url + file, file);
            }
        }
       

        private void gridView1_CustomColumnDisplayText(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnDisplayTextEventArgs e)
        {
            if (e.Column.FieldName == "status")
            {
                if (e.DisplayText == "0")
                    e.DisplayText = "未处理";
                if (e.DisplayText == "1")
                    e.DisplayText = "已处理";
                if (e.DisplayText == "2")
                    e.DisplayText = "延期";
            }
        }
    }
}
